package com.ljl.service.impl;

import com.ljl.entity.SignInLog;
import com.ljl.exception.BadRequestException;
import com.ljl.repository.SignInLogRepository;
import com.ljl.service.SignInLogService;
import com.ljl.service.dto.SignInLogQueryCriteria;
import com.ljl.util.PageUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author 刘家良
 * @date 2022/09/11 20:10
 * @description
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SignInLogServiceImpl implements SignInLogService {

    private final SignInLogRepository signInLogRepository;

    @Override
    public Map<String,Object> queryAll(SignInLogQueryCriteria criteria, Pageable pageable) {
        Page<SignInLog> page = signInLogRepository.findAll(pageable);
//        Page<Account> page = accountRepository.findAll((root, criteriaQuery, cb) -> {
//            List<Predicate> list = new ArrayList<>();
//            Path<Object> name = root.get("username");
//            list.add(cb.equal(name, "ljl"));
//            int size = list.size();
//            return cb.and(list.toArray(new Predicate[size]));
//        },pageable);
        return PageUtil.toPage(page);
    }

    @Override
    public List<SignInLog> queryAll() {
        return signInLogRepository.findAll();
    }

    @Override
    public SignInLog create(SignInLog signInLog) {
        return signInLogRepository.save(signInLog);
    }

    @Override
    public SignInLog update(SignInLog signInLog) {
        if (signInLog.getId() == null) {
            throw new BadRequestException("账户id不能为空");
        }
        return signInLogRepository.save(signInLog);
    }

    @Override
    public void deleteById(Long id) {
        signInLogRepository.deleteById(id);
    }

    @Override
    public void deleteAll(Set<Long> ids) {
        for (Long id : ids) {
            signInLogRepository.deleteById(id);
        }
    }

}
